<?php 
class annonce_mes_annonces extends MX_Controller{
	public $category;
	public function index(){
		$title='Mes annonces ';
		$description='Consultez mes annonces enregistré';
		$keywords='annonces ';
		
		$this->load->helper('cookie');
		$saveAnnonce=$this->input->cookie('annonce');
		//enlever annoncer id dans cookie
		if(isset($_POST) and !empty($_POST)){
			foreach ($_POST as $name=>$value){
				$extract=explode('_',$name);
				if(isset($extract[0]) and $extract[0]=='annonce' and isset($extract[1]) and is_numeric($extract[1])){
					
					if($saveAnnonce!=''){
						$extractId=explode(',',$saveAnnonce);
						foreach ($extractId as $index=>$id){
							if($id==$extract[1]){
								unset($extractId[$index]); break;
							}
						}
						$saveAnnonce=implode(',',$extractId);
						$cookie = array(
								'name'   => 'annonce',
								'value'  => $saveAnnonce,
								'expire' => '15000000',
								'prefix' => ''
						);
						$this->input->set_cookie($cookie);
					}
				}
			}
		}
		
		//load cookie annonce id
		if($saveAnnonce=='')$ids_anonnces=array();
		else {
			$extractId=explode(',',$saveAnnonce);
			$ids_anonnces=array_unique($extractId);
		}
		$annonces=array();

		$product=$this->load->Module('product/show');
		if(!empty($ids_anonnces))
		foreach ($ids_anonnces as $index=>$id_annonce){
			$object=$product->get_info($id_annonce);
			//si les annonces dans cookies existe
			if(is_object($object))$annonces[$id_annonce]=$object;
			else {
				//annonce n'existe plus donc supprimer
				unset($ids_anonnces [$index] );
				$saveAnnonce=implode(',',$ids_anonnces);
				$cookie = array(
						'name'   => 'annonce',
						'value'  => $saveAnnonce,
						'expire' => '15000000',
						'prefix' => ''
				);
				$this->input->set_cookie($cookie);
			}
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'annonces'=>$annonces,
				'filter'=>''
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['mes_annonces']='active';
		
		$this->view(
				array('header',
					'annonce-mes_annonces',
					'footer'), 
				$data); // Display the page 
	}
	public function abonner($to_email){

		global $CFG;
		//to do list annonce par email.
		$abonner = $this->model('annonce_models_creer')->abonner($to_email);
		if( is_array($abonner)){

			//send email
			$email= $this->load->Module('send/email');
			$setting=$this->load->Module('setting/admin');
			$edition=$this->load->Module('setting/edition');
			$setting_admin=$setting->get_admin();
			$edition_send=$edition->get_Edition('envoyer_list_annonce');
			$from_email=$setting_admin['admin_email'];
			$from_name='Caverne.fr';
			
			
			$chercher_annonce=$this->load->Module('annonce/chercher');
			foreach ( $abonner as $row){
				$chercher_annonce->rechercher=$row->search;
				$chercher_annonce->category=$row->category;
				$chercher_annonce->region=$row->region;				
				$deactive=$CFG->config['base_url']."annonce/mes_annonces/desabonner/".urlencode($to_email)."/".$row->id;
				$list_annonce='';

				$subject="";
				$message=$edition_send[0]->setting;
				
				$ids_anonnces=$chercher_annonce->get_list_annonces();
					
				if($ids_anonnces){
				
					$product=$this->load->Module('product/show');
					foreach ($ids_anonnces as $id_annonce){
						$annonces[$id_annonce]=$product->get_info($id_annonce);
						$link=$CFG->config['base_url'].$annonces[$id_annonce]->link_category.'/'.$annonces[$id_annonce]->prd_link.'.html';
						$list_annonce.=$annonces[$id_annonce]-> prd_name." <br/>\r\n";
						$list_annonce.="<a href='$link' >$link</a> <br/>\r\n";
						$list_annonce.="Diffusée le ".$annonces[$id_annonce]->date;
					}

					$message_data['list_annonce']=$list_annonce;
					$message_data['deactive']=$deactive;
					
					foreach ($message_data as $key=>$data)
						if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
						
					$status='send';
					$action='send_to_list_abonner';
					$description='';
					$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action);
				}
			}
		}
		
		
	}
	public function desabonner($email,$id){
		$status='';
		if($email and is_numeric($id))
			$status=$this->model('annonce_models_creer')->desabonner($email,$id);
		
		$title='Désabonner votre alerte';
		$description='Désabonner la recherche automatique par mots-clés';
		$keywords='annonces  automatique  mots-clés ';
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['mes_annonces']='active';
		
		$this->view(
				array('header',
						'annonce-mes_annonces-desaboner',
								'footer'),
				$data); // Display the page
	}
	public function alerte(){
		$alerte=new stdclass;
		$alerte->email=$this->input->post('email');
		$alerte->search=$this->input->post('search');
		$alerte->category=$this->input->post('category');
		$alerte->region=$this->input->post('region');
		$this->model('annonce_models_creer')->alerte($alerte);
		
		$title='Créer une alerte sur cette recherche ';
		$description='Pour créer une recherche automatique par mots-clés';
		$keywords='annonces  automatique  mots-clés ';
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['mes_annonces']='active';
		
		$this->view(
				array('header',
						'annonce-mes_annonces-alerte',
						'footer'),
						$data); // Display the page 
	}
	public function envoyer_liste(){
		$title='Mes annonces ';
		$description='Consultez ';
		$keywords='annonces ';
		global $CFG;
		//to do list annonce par email.
		$to_email=$this->input->post('email');
		$ids_anonnces=$this->model('annonce_models_chercher')->annonces_by_email($to_email);
		$deactive=$list_annonce='';
		if($ids_anonnces){

			$product=$this->load->Module('product/show');
			foreach ($ids_anonnces as $id_annonce){
				$annonces[$id_annonce]=$product->get_info($id_annonce);
				$link=$CFG->config['base_url'].$annonces[$id_annonce]->link_category.'/'.$annonces[$id_annonce]->prd_link.'.html';
				$list_annonce.=$annonces[$id_annonce]-> prd_name." <br/>\r\n";
				$list_annonce.="<a href='$link' >$link</a> <br/>\r\n";
				$list_annonce.="Diffusée le ".$annonces[$id_annonce]->date;
			} 
		}
		//send email
		$email= $this->load->Module('send/email');
		$setting=$this->load->Module('setting/admin');
		$edition=$this->load->Module('setting/edition');
		$setting_admin=$setting->get_admin();
		$edition_send=$edition->get_Edition('envoyer_mes_annonces');
		$from_email=$setting_admin['admin_email'];
		$from_name='Caverne.fr';
		
		$subject="";
		$message=$edition_send[0]->setting;
		
		$message_data['list_annonce']=$list_annonce;
		$message_data['deactive']=$deactive;
		
		foreach ($message_data as $key=>$data){
			if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
		}
		$status='send';
		$action='send_to_list_annonce';
		$description='';
			
		$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
		
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'annonces'=>$annonces,
				'email'=>$to_email
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['mes_annonces']='active';
		
		$this->view(
				array('header',
						'annonce-mes_annonces-envoyer',				
						'footer'),
						$data); // Display the page 
		
	}
}